package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

@Service
public class CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private CartListMapper cartListMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ReItemMapper reItemMapper;


    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }

    public Category getCategory(String categoryId) {
        return categoryMapper.getCategory(categoryId);
    }

    public void deleteCategoryById(String categoryId){
        categoryMapper.deleteCategory(categoryId);
    }

    public void editCategory(Category category){
        categoryMapper.editCategory(category);
    }

    public Product getProduct(String productId) {
        return productMapper.getProduct(productId);
    }

    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.getProductListByCategory(categoryId);
    }

    public void insertProduct(Product product){
        productMapper.insertProduct(product.getProductId(),product.getCategoryId(),product.getName(),product.getDescription());
    }

    public void deleteProduct(String productId){
        productMapper.deleteProduct(productId);
    }

    public void editProduct(Product product){
        productMapper.editProduct(product.getProductId(),product.getCategoryId(),product.getName(),product.getDescription());
    }

    // TODO enable using more than one keyword
    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    public List<Item> searchItemList(String keyword) {
        return itemMapper.searchItemList("%" + keyword.toLowerCase() + "%");
    }

    public List<Category> searchCategoryList(String keyword) {
        return categoryMapper.searchCategoryList("%" + keyword.toLowerCase() + "%");
    }

    public String SearchAjax(String keyword) {
        String res = "";
        List<Product> productList = productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
        for (int i = 0; i < productList.size(); i++) {
            if (i > 0) {
                res += "," + productList.get(i).getProductId();
            } else {
                res += productList.get(i).getProductId();
            }
        }
        return res;
    }

    public List<Item> getItemListByProduct(String productId) {
        return itemMapper.getItemListByProduct(productId);
    }

    public void updateItem(Item item){itemMapper.updateItem(item);}

    public void updateInventory(Item item){itemMapper.updateInventory(item);}

    public void addItem(Item item){itemMapper.addItem(item);}

    public void addInventory(Item item){itemMapper.addInventory(item);}

    public Item getItem(String itemId) {
        return itemMapper.getItem(itemId);
    }

    public void deleteItem(Item item){itemMapper.deleteItem(item);}

    public boolean isItemInStock(String itemId) {
        return itemMapper.getInventoryQuantity(itemId) > 0;
    }

    public void insertCartList(CartList cartList) {
        cartListMapper.insertCartList(cartList);
    }

    public void deleteCartListByUsername(String username) {
        cartListMapper.deleteCartByUsername(username);
    }

//    public List<CartList> getCartListByUsername(String username) {
//        return cartListMapper.getCartListByUsername(username);
//    }
    public List<CartItem> getCartItemListByUserName(String userid){
        return cartMapper.getCartItemListByUserName(userid);
    }

    public void deleteCartItemByUsernameAndItemid(String username, String itemid) {
        cartListMapper.deleteCartItemByUsernameAndItemId(username, itemid);
    }

    public void incrementQuantityByItemId(int quantity,BigDecimal totalprice,String userId,String itemId) {
        cartMapper.incrementQuantityByItemId(quantity,totalprice,userId,itemId);
    }

    public int getQuantityByItemAndUserId(String userId,String itemId)
    {
        return cartMapper.getQuantityByItemAndUserId(userId,itemId);
    }

    public BigDecimal getListPriceByItemAndUserId(String userId, String itemId)
    {
        return cartMapper.getListPriceByItemAndUserId(userId,itemId);
    }
    public void insertAccount(String userId,Item item) {
        cartMapper.insertAccount(userId,item.getItemId(),item.getProductId(),item.getAttribute1(), true,1,item.getListPrice(),item.getListPrice());
    }
    public void removeAllByUserName(String userId){
        cartMapper.removeAllByUserName(userId);
    }
    public void updateCartItemQuantityAjax(String username,String itemId,int quantity) {
        cartMapper.update(itemId,username,quantity);
    }
//    public void insertReItem(ReItem record){reItemMapper.insertReItem(record);}
public int getQTYFromInventory(String itemId) {
    return categoryMapper.getInventory(itemId);
}
    public void updateInventory(String itemId, int qty) {
//        int q = categoryMapper.getInventory(itemId);
//        System.out.println("num is "+q);
//        q--;
        categoryMapper.updateInventory(itemId,qty);
    }

    public void insertCategory(String catId,String name,String desc){
        categoryMapper.insertCategory(catId,name,desc);
    }

    public void deleteProductByCategoryId(String categoryId) {
        productMapper.deleteProductByCategoryId(categoryId);
    }

    public void deleteItemByProductId(String productId){
        itemMapper.deleteItemByProductId(productId);
    }
}
